﻿@using System.Globalization
@using MvcFront.Models
@using Telerik.Web.Mvc.UI
@using MvcFront.DB
@model IEnumerable<FileLibraryFolder>
           
@{
    ViewBag.Title = "Библиотека";
}
   @Html.ValidationSummary(true)
<table><tr>
           <td valign="top" width="20%">
               <h3>Раздел</h3>
               @(Html.Telerik().TreeView().ExpandAll(true).Items(i => i.Add().Value("0").Text("Все разделы"))
                .Name("FoldersTreeView")
                .ClientEvents(events => events.OnSelect("onFolderSelected"))
                        .BindTo(Model, mappings => mappings.For<FileLibraryFolder>(binding => binding
                                                                               .ItemDataBound((item, category) =>
                                                                                                  {
                                                                                                      item.Text = category.Name;
                                                                                                      item.Value = category.filelibraryfolderid.ToString(CultureInfo.InvariantCulture);
                                                                                                  })
                                                                               .Children(category => category.ChildFileLibraryFolders)))
                     ) 
           </td>
           <td>
                <p>
                    <label for="contains"><strong>Название (комментарий)</strong> содержит:</label>
                    @Html.TextBox("contains", "") 
                    <button class="t-button t-state-default" onclick="rebindGrid();">Искать</button>
                     <button class="t-button t-state-default" onclick="clearText();">Очистить</button>
                </p>
    
               

               @(Html.Telerik().Grid<FileLibraryAssetListViewModel>()
                .Name("AssetsListGrid")
                .DataBinding(dataBinding => dataBinding.Ajax().Select("_AssetsFromFolder", "Library"))
                .ClientEvents(events => events.OnDataBinding("onGridDataBinding"))
                .Columns(columns =>
                        {
                            columns.AutoGenerate(column =>
                                                     {
                                                         column.Width = "200px";                                                         
                                                         if (column.Member == "AssetId" || column.Member == "FileName")
                                                             column.Visible = false;
                                                     });
                            columns.Command(command => command.Custom("viewDownload").Text("Скачать")
                                                           .DataRouteValues(route => route.Add(o => o.AssetId).RouteKey("assetId"))
                                                           .Action("Download", "Library")
                                                           .HtmlAttributes(new { style = "text-align: center" })).Width(100);
                        })
           .Scrollable(c => c.Height("400px"))
           .Sortable()
           .Pageable()

                     ) 
           </td>
       </tr>
</table>

<script type="text/javascript">

    var selectedFolder = 0;
    
    function onFolderSelected(e) {
        selectedFolder = $('#FoldersTreeView').data('tTreeView').getItemValue(e.item);
        var grid = $('#AssetsListGrid').data("tGrid");
        if (grid != null) {
            grid.rebind();
        }
    }

    function rebindGrid() {
        var grid = $('#AssetsListGrid').data("tGrid");
        if (grid != null) {
            grid.rebind();
        }
    }

    function clearText() {
        $("#contains").val('');
        rebindGrid();
    }

    function onGridDataBinding(e) {

        e.data = { id: selectedFolder, text: $("#contains").val() };
    }
    
</script>
